from random import choice

class Bug:
    
    #constructor
    def __init__(self):
        # init list where we store all CE-Chains in relation with each other
        self.__listOfCEChains = []
    
    # add another CEChain to this bug    
    def addNewCEChain(self, chain):
        self.__listOfCEChains.append(chain)
       
    # get the size of the CEChain-list
    def size(self):
        return len(self.__listOfCEChains)   
       
    # get diff list from any CEChain
    def getAnyCauseChain(self):
        assert self.__listOfCEChains # not None and not empty
        return choice(self.__listOfCEChains).getCauseEffectChain()
    
    # get diff list from any CEChain
    def getMinCauseChain(self):
        assert self.__listOfCEChains # not None and not empty
        return min(self.__listOfCEChains,key=len)
    
    # returns an value which should describe if the chain matches the minimalCauseChain
    # the bigger the result the bigger the chance that the chain is related to this bug
    def getMatchingValue(self, chain):
        # checks for both None and empty
        assert chain
        # init the overall value
        overallValue = 0
        
        ##############################
        # check for line coverage
        ##############################
        
        # get the coverage of the argument chain
        chainCoverage = chain.getFailingCoverage()
        # run over all CEChains stored in this bug
        for localChain in self.__listOfCEChains:
            localValue = 0
            localCoverage = localChain.getFailingCoverage()
            diffCoverage = localCoverage.intersection(chainCoverage)
            localValue = ( len(diffCoverage) / len(chainCoverage) ) * 50
            overallValue += localValue
        
        overallValue = overallValue / len(self.__listOfCEChains)
        
        #############################
        # returns result
        #############################
        
        return overallValue
            